/**
 * Created by DevilsEyes on 2016/4/19.
 */
'use strict';

var md5 = require('blueimp-md5');
var $user = require('../db/user.js');

let v = (v, def)=>v === undefined ? def : v;

/*
 注册流程

 用户输入
 id,name,email,password提交
 id不可重复

 服务器创建
 根据以上参数创建新用户
 role=-2

 后台审核
 审核后，发送验证邮件给目标邮箱，以Mongo的_id作为KEY。
 role=-1

 用户进入验证页
 凭借_id获取信息
 补全nickname

 服务器修改
 凭借id,password生成secId,secPassword
 role=1

 */


module.exports = {

    sync_filter: (user)=> {
        return {
            secId: user.secId,
            name: user.name,
            class: user.class,
            nickname: user.nickname,
            email: user.email
        }
    },

    regist: (obj) => {

        let id = obj.id;
        let name = obj.name;
        let email = obj.email;
        let Class = obj.class;

        return new Promise((O, X)=> {

            if (isNaN(id)) {
                X({
                    code: 9999,
                    msg: '无效的ID'
                })
            }

            var user_Entity = new $user({
                id: id,//学号
                secId: md5(id),//md5学号
                password: '',//密码
                secPassword: '',//md5密码
                role: -2,//权限
                class: Class,//班级

                name: name,//姓名
                email: email,//邮箱
                nickname: ''//昵称
            });

            user_Entity.save(()=> {
                O()
            })
        })
    },

    searchByMd5Id: (md5_id)=> {

        return new Promise((O, X)=> {
            $user
                .find({secId: md5_id})
                .then((users) => {
                    users = users || [];
                    if (users.length > 0) {
                        O(users[0]);
                    }
                    else {
                        X({
                            code: 9999,
                            msg: '错误的 SecId'
                        })
                    }
                });
        })
    },

    getUserByMongoId: (_id)=> {
        return new Promise((O, X)=> {
            $user
                .find({_id: _id})
                .then((users) => {
                    users = users || [];
                    if (users.length > 0) {
                        O(users[0]);
                    }
                    else {
                        X({
                            code: 9999,
                            msg: '数据库查询失败'
                        })
                    }
                });
        })
    },

    complete: (options)=> {

        let _id = options._id;

        return new Promise((O, X)=> {
            $user
                .findById(_id)
                .then((user) => {
                    if (user) {

                        user.nickname = options.nickname;
                        user.password = options.password;
                        user.secPassword = md5(options.password);
                        user.role = 1;
                        user.save(function () {
                            O(user);
                        });
                    }
                    else {
                        X({
                            code: 9999,
                            msg: '数据库查询失败'
                        })
                    }
                });
        })

    },

    // update: (id, obj) => {
    //     var cp = {};
    //     if (obj.title)cp.title = obj.title;
    //     if (obj.link)cp.link = obj.link;
    //     if (obj.status)cp.status = obj.status;
    //     if (obj.weight)cp.weight = obj.weight;
    //
    //     return $user
    //         .find({id: id})
    //         .update(cp)
    // },

    // 查重
    isRepeated(id){
        return new Promise((O, X)=> {
            $user
                .find({id: id})
                .then((users) => {
                    if (!users) {
                        X({
                            code: 9999,
                            msg: '查重-数据查询失败'
                        })
                    }
                    if (users.length > 0) {
                        X({
                            code: 9999,
                            msg: '学号重复'
                        })
                    }
                    else {
                        O();
                    }
                });
        })
    }


};